package com.xpgk.mvc.measurement;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xpgk.dto.RenderBean;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.bentryRegister.CustomerAgreement;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.productPrice.Price;
import com.xpgk.mvc.productPrice.ProductPrice;
import com.xpgk.mvc.scallVisit.ScallVisit;

public class MeasurementController extends BaseController {

	private TemporaryServices temporaryService = new TemporaryServices();

	public void index() {
		// 获取当前机构id
		String organids = getCUserOrganIds();
		String userids = getCUserIds();
		// 编号
		String number = temporaryService.getNumber(organids);
		if (!number.equals("false")) {
			setAttr("number", number);
		} else {
			setAttr("number", "null");
		}
		Organization organization = Organization.dao.findById(organids);
		String dlsIds = organization.getStr(Organization.column_parentIds);
		String sqlPriceIds = "SELECT * FROM sb_shop_price where organizationIds = ?";
		Price price = Price.dao.findFirst(sqlPriceIds,dlsIds);
		String priceIds = price.getStr(Price.column_ids);
		String sqlProductPrice = "SELECT * FROM sb_product_price where priceIds = ? and productIds = ? and status = '1'";
		
		float bzlc = ProductPrice.dao.findFirst(sqlProductPrice,priceIds,Code.bzlc_ids).getFloat("outPrice");
		float ylc = ProductPrice.dao.findFirst(sqlProductPrice,priceIds,Code.ylcdsh_ids).getFloat("outPrice");
		setAttr("bzlc", bzlc);
		setAttr("ylc", ylc);
		setAttr("organids", organids);
		setAttr("userids", userids);
		render("consultMesureCalculate.html");
	}

	/*
	 * 今日测算查询
	 */
	public void todaymeasure() {
		// 获取当前机构id
		String todaydate = getPara("todaydate");
		String organids = getCUserOrganIds();
		List<Record> result = temporaryService.todaymeasure(organids, todaydate);
		renderJson(result);
	}

	/*
	 * 今日测算查询页面跳转
	 */
	public void todaymeasureindex() {
		// setAttr("curOrgan", Organization.dao.findById(getCUserOrganIds()));
		String todaydate = getPara("todaydate");
		setAttr("todaydate", todaydate);
		render("todayMeasurementQuery.html");
	}

	/**
	 * 测算
	 */
	@Before({ TemporaryValidator.class, Tx.class })
	public void cesuan() {
		// 获取当前人员所属机构ids
		String organids = getCUserOrganIds();
		Temporary temp = getModel(Temporary.class, "temporary");
		if (temp != null) {
			setSessionAttr(Temporary.column_ids + temp.getInt("ids"), temp);
		}
		Map<String, Object> mm = temporaryService.saveRecord(temp, organids);
		renderJson(mm);
	}

	/**
	 * 获取介绍人门店
	 * 
	 */

	public void getJsrStore() {
		String jsrPhone = getPara("jsrPhone");
		RenderBean result = temporaryService.getJsrStore(jsrPhone);
		renderJson(result);

	}

	/**
	 * 保存
	 */
	@Before({ TemporaryValidator.class, Tx.class })
	public void saveTemp() {
		// 获取当前登录机构id
		String cudid = getCUserOrganIds();
		String userIds = getCUserIds();
		Temporary temp = getModel(Temporary.class, "temporary");
		CustomerAgreement cusAgr = getModel(CustomerAgreement.class);
		String delWeight = getPara("delWeight");
		String targetWeight = getPara("targetWeight");
		String chargePersion = getPara("chargePersion");
		
		RenderBean result = temporaryService.saveTempAndCus( temp, cusAgr, delWeight, targetWeight, cudid,chargePersion, userIds);
		renderJson(result.getMap());
	}

	/**
	 * 用手机号搜索顾客测算信息
	 */
	public void phoneSearch() {
		String phone = getPara("phone");
		String name = getPara("name");
		// 获取当前登录机构id
		String orgid = getCUserOrganIds();
		Map<String, Object> mm = temporaryService.phoneSearch(name, phone, orgid);
		
		
		renderJson(mm);
	}

	/**
	 * 查看上一条下一条
	 */
	public void selOther() {
		String number = getPara("number");
		String obj = getPara("obj");
		// 获取当前机构id
		String organids = getCUserOrganIds();
		Temporary temp = temporaryService.selOther(number, obj, organids);
		if (temp != null) {
			setAttr("number", temp.getStr(Temporary.column_number));
			setAttr("temporary", temp);
			setAttr("userids", temp.getStr(Temporary.column_jdr));
		} else {
			setAttr("number", temporaryService.getNumber(organids));
			setAttr("temporary", temp);
		}
		// index();
		// redirect("/measurement");
		setAttr("organids", organids);
		render("consultMesureCalculate.html");
	}

	/*
	 * 查询顾客到店回访信息信息
	 */
	public void querypeople() {
		String temporaryCustomerIds = getPara("temporaryCustomerIds");
		String organIds = getCUserOrganIds();
		String sql = getSqlMy("sb.temporary.querypeople");
		List<Record> result = Db.find(sql, temporaryCustomerIds, 2, organIds);
		renderJson(result);
	}

	/*
	 * 查询顾客电话回访信息
	 */
	public void querypeoplefromcall() {
		String temporaryCustomerIds = getPara("temporaryCustomerIds");
		String organIds = getCUserOrganIds();
		String sql = getSqlMy("sb.temporary.querypeople");
		List<Record> result = Db.find(sql, temporaryCustomerIds, 1, organIds);
		renderJson(result);
	}

	/**
	 * 获取会员名字/id
	 * 
	 */
	public void getCusInfo() {
		// 获取当前登录机构id
		String orgid = getCUserOrganIds();
		List<Temporary> mm = temporaryService.getCusInfo(orgid);
		renderJson(mm);
	}
	
	/**
	 * 获取会员及用户信息/id
	 * 
	 * */
	public void  getCusAndUserInfo(){
		List<Temporary>  mm=temporaryService.getCusAndUserInfo();
		renderJson(mm);
	}
	

}
